<?php
/**[属性模型]
 * @Author: 976123967@qq.com
 * @Date:   2014-11-08 15:01:39
 * @Last Modified by:   chenryoku
 * @Last Modified time: 2014-11-15 18:40:59
 */
class AttrModel extends Model{

	protected $tableName = 'Attr';
	private $attr;

	/**
	 * [_initialize 初始化]
	 * @return [type] [description]
	 */
	public function _initialize()
	{
		$this->attr = S('attr');
	}

	/**
	 * [$_auto 自动完成]
	 * @var array
	 */
	protected $_auto = array(
		array('type_typeid','_typeid',3,'callback'),
		array('sort','intval',3,'function'),
	);
	/**
	 * [_typeid type_typeid自动完成]
	 * @return [type] [description]
	 */
	protected function _typeid()
	{
		return I('typeid',null,'intval');
	}

	/**
	 * [get_attr_all 所有属性]
	 * @return [type] [description]
	 */
	public function get_attr_all($typeid)
	{
		$data = $this->attr[$typeid];
		if(!$data) return false;
		foreach($data as $k=>$v)
		{
			$data[$k]['type'] = $v['type']==1 ?'单选':'多选';
		}
		return $data;
	}

	public function add_attr()
	{
		// 添加属性
		$attrId = $this->add();

		// 添加属性值
		$data = array();
		$value =  I('post.attr_value');

		$attrValueModel =  D('AttrValue');
		if($value)
		{
			foreach($value as $v)
			{
				if($v)
				{
					$data = array(

					'attr_attr_id'=>$attrId,
					'attr_value'=>$v,

					);
				}

				$attrValueModel->add_attr_value($data);
				
			}
		}
		
		if(!empty($data))
			
		$this->update_cache();
	}


	/**
	 * [edit_attr 编辑]
	 * @return [type] [description]
	 */
	public function edit_attr()
	{
		$this->save($data);


		$attrId =I('post.attr_id');
		// 删除当前属性的所有值
		D('AttrValue')->del_attr_value_by_attr_id($attrId);


		// 添加属性值
		$data = array();
		$value =  $_POST['attr_value'];
		

		$attrValueModel =  D('AttrValue');
		if($value)
		{
			foreach($value as $v)
			{


				if(is_array($v))
				{

					if(current($v))
					{
						$data = array(
							'attr_value_id'=>key($v),
							'attr_attr_id'=>$attrId,
							'attr_value'=>current($v),

						);
					}
				}
				else
				{
					if($v)
					{
						$data = array(

						'attr_attr_id'=>$attrId,
						'attr_value'=>$v,

						);
					}
				}

				$attrValueModel->add_attr_value($data);
			}
		}
		
		$this->update_cache();

	}

	

	/**
	 * [find_attr_one 单条属性]
	 * @return [type] [description]
	 */
	public function find_attr_one()
	{
		$fields = $this->attr[I('get.typeid')];
		foreach($fields as $v)
		{
			if($v['attr_id']==I('get.attr_id'))
			{
				return $v;
			}

		}
		return false;
	}


	/**
	 * [update_cache 更新缓存]
	 * @return [type] [description]
	 */
	public function update_cache()
	{
		$data = $this->order('sort asc')->select();

		$attrValueModel = D('AttrValue');
		$temp = array();
		if($data)
		{
			foreach($data as $v)
			{
				$v['attr_value'] = $attrValueModel->get_attr_value_all($v['attr_id']);
				$temp[$v['type_typeid']][]=$v;
				
			}
		}
		S('attr',$temp);
	}



	/**
	 * [update_order 排序]
	 * @return [type] [description]
	 */
	public function update_order()
	{
		// 验证
		$attrId = I('post.attr_id');
		if(empty($attrId))
		{
			$this->error='没有信息用于排序';
			return false;
		}

		$listOrder= I('post.list_order');
		// 循环更新
		foreach($attrId as $k=>$v)
		{
			$data =array(
				'sort'=>$listOrder[$k]
			);
			$this->where(array('attr_id'=>$v))->save($data);
		}
		$this->update_cache();
		return true;
	}


	/**
	 * [delete_check 删除]
	 * @return [type] [description]
	 */
	public function delete_check()
	{
		// 验证
		$attrId = I('post.attr_id');
		if(empty($attrId))
		{
			$this->error='没有信息用于删除';
			return false;
		}
		// 删除
		foreach($attrId as $k=>$v)
		{
			$this->delete_attr($v);
		}
		$this->update_cache();
		return true;
	}


	/**
	 * [delete_attr 删除属性和属性值]
	 * @param  [type] $attrId [description]
	 * @return [type]         [description]
	 */
	public function delete_attr($attrId)
	{
		$this->delete($attrId);
		// 删除当前属性的所有值
		D('AttrValue')->del_attr_value_by_attr_id($attrId);
		$this->update_cache();
		
	}

	/**
	 * [get_attr_form 获取属性表单]
	 * @param  [type] $typeid [description]
	 * @return [type]         [description]
	 */
	public function get_attr_form($typeid,$aid=0)
	{
		$fields = $this->attr[$typeid];
		//p($fields);die;
		$form  = array();
		if(!empty($fields))
		{

			$articleAttrModel = D('ArticleAttr');
			$articleAttr = array();
			if($aid)
			{
				// 编辑时获取已经选择的属性值attr_value_attr_value_id
				$articleAttr = $articleAttrModel->find_article_attr($aid);
			}
			//p($articleAttr);die;
		

			foreach ($fields as $k=>$v) 
			{
				$result = '';
				foreach($v['attr_value'] as $f)
				{	
					
					// 判断选中状态
					$checked = in_array($f['attr_value_id'],$articleAttr)?'checked="chekced"':'';
					$articleAttrId = 0;
					if($checked)
					{
						// 选中了就一定有文档属性的主键article_attr_id
						$articleAttrId = $articleAttrModel->get_field_by_attr_value_id($f['attr_value_id'],$aid);
						//p($f);die;
						//echo $articleAttrModel->_sql();die;
					}


					// 验证是否有图集的信息
					$onclick = '';
					if($v['is_pic'])
						$onclick = "onclick='set_article_pic($f[attr_value_id],\"$f[attr_value]\",this)'";

					$result .= "<label ><input {$onclick} {$checked} type='checkbox'  name='article_attr[{$v[attr_id]}-{$v[type]}-{$v[type_typeid]}-{$v[is_pic]}-$articleAttrId][]' value='{$f[attr_value]}' /> {$f[attr_value]}</label>&nbsp;&nbsp;&nbsp;";
				}
		

				$form[] = array('html'=>$result,'title'=>$v['attr_name']);
			}
		}

		return $form;
	}


	/**
	 * [check_attr_by_tyleid 验证是否有属性通过文档类型typeid,删除类型时候验证]
	 * @param  [type] $typeid [description]
	 * @return [type]         [description]
	 */
	public function check_attr_by_tyleid($typeid)
	{
		if($this->where(array('type_typeid'=>$typeid))->count()) 
			return true;
		else
			return false;
	}

	/**
	 * [check_attr_pic 验证类型中是否有关联图集的属性，开启属性图集]
	 * @param  [type] $typeid [description]
	 * @return [type]         [description]
	 */
	public function check_attr_pic($typeid)
	{
		$fields = $this->attr[$typeid];
		if(!$fields) return false;
		$count = 0;
		foreach($fields as $v)
		{
			if($v['is_pic'])
				$count++;
		}

		//p($count);die;
		return $count;
	}

	/**
	 * [check_is_attr_pic 验证当前是否是有图片集的属性]
	 * @param  [type] $attrId [description]
	 * @return [type]         [description]
	 */
	public function check_is_attr_pic($attrId)
	{
		return $this->where(array('attr_id'=>$attrId))->getField('is_pic');
	}
}